home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / Berkeley DB 1.8.5a / hash / hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  23.8 KB  |  1,006 lines  |  [TEXT/CWIE]

  1. /*-
  2.  * Copyright (c) 1990, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Margo Seltzer.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)hash.c    8.9 (Berkeley) 6/16/94";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #include <sys/param.h>
  42. #include <sys/stat.h>
  43.  
  44. #include <errno.h>
  45. #include <fcntl.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <unistd.h>
  50. #ifdef DEBUG
  51. #include <assert.h>
  52. #endif
  53.  
  54. #include <db.h>
  55. #include "hash.h"
  56. #include "page.h"
  57. #include "hash_extern.h"
  58.  
  59. static int   alloc_segs __P((HTAB *, int));
  60. static int   flush_meta __P((HTAB *));
  61. static int   hash_access __P((HTAB *, ACTION, DBT *, DBT *));
  62. static int   hash_close __P((DB *));
  63. static int   hash_delete __P((const DB *, const DBT *, u_int32_t));
  64. static int   hash_fd __P((const DB *));
  65. static int   hash_get __P((const DB *, const DBT *, DBT *, u_int32_t));
  66. static int   hash_put __P((const DB *, DBT *, const DBT *, u_int32_t));
  67. static void *hash_realloc __P((SEGMENT **, int, int));
  68. static int   hash_seq __P((const DB *, DBT *, DBT *, u_int32_t));
  69. static int   hash_sync __P((const DB *, u_int32_t));
  70. static int   hdestroy __P((HTAB *));
  71. static HTAB *init_hash __P((HTAB *, const char *, HASHINFO *));
  72. static int   init_htab __P((HTAB *, int));
  73. #if BYTE_ORDER == LITTLE_ENDIAN
  74. static void  swap_header __P((HTAB *));
  75. static void  swap_header_copy __P((HASHHDR *, HASHHDR *));
  76. #endif
  77.  
  78. /* Fast arithmetic, relying on powers of 2, */
  79. #define MOD(x, y)        ((x) & ((y) - 1))
  80.  
  81. #define RETURN_ERROR(ERR, LOC)    { save_errno = ERR; goto LOC; }
  82.  
  83. /* Return values */
  84. #define    SUCCESS     (0)
  85. #define    ERROR    (-1)
  86. #define    ABNORMAL (1)
  87.  
  88. #ifdef HASH_STATISTICS
  89. int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
  90. #endif
  91.  
  92. /************************** INTERFACE ROUTINES ***************************/
  93. /* OPEN/CLOSE */
  94.  
  95. extern DB *
  96. __hash_open(file, flags, mode, info, dflags)
  97.     const char *file;
  98.     int flags, mode, dflags;
  99.     const HASHINFO *info;    /* Special directives for create */
  100. {
  101.     HTAB *hashp;
  102.     struct stat statbuf;
  103.     DB *dbp;
  104.     int bpages, hdrsize, new_table, nsegs, save_errno;
  105.  
  106.     if ((flags & O_ACCMODE) == O_WRONLY) {
  107.         errno = EINVAL;
  108.         return (NULL);
  109.     }
  110.  
  111.     if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
  112.         return (NULL);
  113.     hashp->fp = -1;
  114.  
  115.     /*
  116.      * Even if user wants write only, we need to be able to read
  117.      * the actual file, so we need to open it read/write. But, the
  118.      * field in the hashp structure needs to be accurate so that
  119.      * we can check accesses.
  120.      */
  121.     hashp->flags = flags;
  122.  
  123.     new_table = 0;
  124.     if (!file || (flags & O_TRUNC) ||
  125.         (stat(file, &statbuf) && (errno == ENOENT))) {
  126.         if (errno == ENOENT)
  127.             errno = 0; /* Just in case someone looks at errno */
  128.         new_table = 1;
  129.     }
  130.     if (file) {
  131.         if ((hashp->fp = DB_open(file, flags, mode)) == -1)
  132.             RETURN_ERROR(errno, error0);
  133. #ifndef macintosh
  134.         (void)fcntl(hashp->fp, F_SETFD, 1);
  135. #endif
  136.     }
  137.     if (new_table) {
  138.         if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
  139.             RETURN_ERROR(errno, error1);
  140.     } else {
  141.         /* Table already exists */
  142.         if (info && info->hash)
  143.             hashp->hash = info->hash;
  144.         else
  145.             hashp->hash = __default_hash;
  146.  
  147.         hdrsize = DB_read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
  148. #if BYTE_ORDER == LITTLE_ENDIAN
  149.         swap_header(hashp);
  150. #endif
  151.         if (hdrsize == -1)
  152.             RETURN_ERROR(errno, error1);
  153.         if (hdrsize != sizeof(HASHHDR))
  154.             RETURN_ERROR(EFTYPE, error1);
  155.         /* Verify file type, versions and hash function */
  156.         if (hashp->MAGIC != HASHMAGIC)
  157.             RETURN_ERROR(EFTYPE, error1);
  158. #define    OLDHASHVERSION    1
  159.         if (hashp->VERSION != HASHVERSION &&
  160.             hashp->VERSION != OLDHASHVERSION)
  161.             RETURN_ERROR(EFTYPE, error1);
  162.         if (hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
  163.             RETURN_ERROR(EFTYPE, error1);
  164.         /*
  165.          * Figure out how many segments we need.  Max_Bucket is the
  166.          * maximum bucket number, so the number of buckets is
  167.          * max_bucket + 1.
  168.          */
  169.         nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
  170.              hashp->SGSIZE;
  171.         hashp->nsegs = 0;
  172.         if (alloc_segs(hashp, nsegs))
  173.             /*
  174.              * If alloc_segs fails, table will have been destroyed
  175.              * and errno will have been set.
  176.              */
  177.             return (NULL);
  178.         /* Read in bitmaps */
  179.         bpages = (hashp->SPARES[hashp->OVFL_POINT] +
  180.             (hashp->BSIZE << BYTE_SHIFT) - 1) >>
  181.             (hashp->BSHIFT + BYTE_SHIFT);
  182.  
  183.         hashp->nmaps = bpages;
  184.         (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
  185.     }
  186.  
  187.     /* Initialize Buffer Manager */
  188.     if (info && info->cachesize)
  189.         __buf_init(hashp, info->cachesize);
  190.     else
  191.         __buf_init(hashp, DEF_BUFSIZE);
  192.  
  193.     hashp->new_file = new_table;
  194. #ifdef macintosh
  195.     hashp->save_file = file && ((flags & O_ACCMODE) == O_RDWR);
  196. #else
  197.     hashp->save_file = file && (hashp->flags & O_RDWR);
  198. #endif
  199.     hashp->cbucket = -1;
  200.     if (!(dbp = (DB *)malloc(sizeof(DB)))) {
  201.         save_errno = errno;
  202.         hdestroy(hashp);
  203.         errno = save_errno;
  204.         return (NULL);
  205.     }
  206.     dbp->internal = hashp;
  207.     dbp->close = hash_close;
  208.     dbp->del = hash_delete;
  209.     dbp->fd = hash_fd;
  210.     dbp->get = hash_get;
  211.     dbp->put = hash_put;
  212.     dbp->seq = hash_seq;
  213.     dbp->sync = hash_sync;
  214.     dbp->type = DB_HASH;
  215.  
  216. #ifdef DEBUG
  217.     (void)fprintf(stderr,
  218. "%s\n%s%x\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
  219.         "init_htab:",
  220.         "TABLE POINTER   ", hashp,
  221.         "BUCKET SIZE     ", hashp->BSIZE,
  222.         "BUCKET SHIFT    ", hashp->BSHIFT,
  223.         "DIRECTORY SIZE  ", hashp->DSIZE,
  224.         "SEGMENT SIZE    ", hashp->SGSIZE,
  225.         "SEGMENT SHIFT   ", hashp->SSHIFT,
  226.         "FILL FACTOR     ", hashp->FFACTOR,
  227.         "MAX BUCKET      ", hashp->MAX_BUCKET,
  228.         "OVFL POINT         ", hashp->OVFL_POINT,
  229.         "LAST FREED      ", hashp->LAST_FREED,
  230.         "HIGH MASK       ", hashp->HIGH_MASK,
  231.         "LOW  MASK       ", hashp->LOW_MASK,
  232.         "NSEGS           ", hashp->nsegs,
  233.         "NKEYS           ", hashp->NKEYS);
  234. #endif
  235. #ifdef HASH_STATISTICS
  236.     hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
  237. #endif
  238.     return (dbp);
  239.  
  240. error1:
  241.     if (hashp != NULL)
  242.         (void)close(hashp->fp);
  243.  
  244. error0:
  245.     free(hashp);
  246.     errno = save_errno;
  247.     return (NULL);
  248. }
  249.  
  250. static int
  251. hash_close(dbp)
  252.     DB *dbp;
  253. {
  254.     HTAB *hashp;
  255.     int retval;
  256.  
  257.     if (!dbp)
  258.         return (ERROR);
  259.  
  260.     hashp = (HTAB *)dbp->internal;
  261.     retval = hdestroy(hashp);
  262.     free(dbp);
  263.     return (retval);
  264. }
  265.  
  266. static int
  267. hash_fd(dbp)
  268.     const DB *dbp;
  269. {
  270.     HTAB *hashp;
  271.  
  272.     if (!dbp)
  273.         return (ERROR);
  274.  
  275.     hashp = (HTAB *)dbp->internal;
  276.     if (hashp->fp == -1) {
  277.         errno = ENOENT;
  278.         return (-1);
  279.     }
  280.     return (hashp->fp);
  281. }
  282.  
  283. /************************** LOCAL CREATION ROUTINES **********************/
  284. static HTAB *
  285. init_hash(hashp, file, info)
  286.     HTAB *hashp;
  287.     const char *file;
  288.     HASHINFO *info;
  289. {
  290.     struct stat statbuf;
  291.     int nelem;
  292.  
  293.     nelem = 1;
  294.     hashp->NKEYS = 0;
  295.     hashp->LORDER = BYTE_ORDER;
  296.     hashp->BSIZE = DEF_BUCKET_SIZE;
  297.     hashp->BSHIFT = DEF_BUCKET_SHIFT;
  298.     hashp->SGSIZE = DEF_SEGSIZE;
  299.     hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
  300.     hashp->DSIZE = DEF_DIRSIZE;
  301.     hashp->FFACTOR = DEF_FFACTOR;
  302.     hashp->hash = __default_hash;
  303.     memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
  304.     memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
  305.  
  306.     /* Fix bucket size to be optimal for file system */
  307.     if (file != NULL) {
  308.         if (stat(file, &statbuf))
  309.             return (NULL);
  310.         hashp->BSIZE = statbuf.st_blksize;
  311.         hashp->BSHIFT = __log2(hashp->BSIZE);
  312.     }
  313.  
  314.     if (info) {
  315.         if (info->bsize) {
  316.             /* Round pagesize up to power of 2 */
  317.             hashp->BSHIFT = __log2(info->bsize);
  318.             hashp->BSIZE = 1 << hashp->BSHIFT;
  319.             if (hashp->BSIZE > MAX_BSIZE) {
  320.                 errno = EINVAL;
  321.                 return (NULL);
  322.             }
  323.         }
  324.         if (info->ffactor)
  325.             hashp->FFACTOR = info->ffactor;
  326.         if (info->hash)
  327.             hashp->hash = info->hash;
  328.         if (info->nelem)
  329.             nelem = info->nelem;
  330.         if (info->lorder) {
  331.             if (info->lorder != BIG_ENDIAN &&
  332.                 info->lorder != LITTLE_ENDIAN) {
  333.                 errno = EINVAL;
  334.                 return (NULL);
  335.             }
  336.             hashp->LORDER = info->lorder;
  337.         }
  338.     }
  339.     /* init_htab should destroy the table and set errno if it fails */
  340.     if (init_htab(hashp, nelem))
  341.         return (NULL);
  342.     else
  343.         return (hashp);
  344. }
  345. /*
  346.  * This calls alloc_segs which may run out of memory.  Alloc_segs will destroy
  347.  * the table and set errno, so we just pass the error information along.
  348.  *
  349.  * Returns 0 on No Error
  350.  */
  351. static int
  352. init_htab(hashp, nelem)
  353.     HTAB *hashp;
  354.     int nelem;
  355. {
  356.     register int nbuckets, nsegs;
  357.     int l2;
  358.  
  359.     /*
  360.      * Divide number of elements by the fill factor and determine a
  361.      * desired number of buckets.  Allocate space for the next greater
  362.      * power of two number of buckets.
  363.      */
  364.     nelem = (nelem - 1) / hashp->FFACTOR + 1;
  365.  
  366.     l2 = __log2(MAX(nelem, 2));
  367.     nbuckets = 1 << l2;
  368.  
  369.     hashp->SPARES[l2] = l2 + 1;
  370.     hashp->SPARES[l2 + 1] = l2 + 1;
  371.     hashp->OVFL_POINT = l2;
  372.     hashp->LAST_FREED = 2;
  373.  
  374.     /* First bitmap page is at: splitpoint l2 page offset 1 */
  375.     if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
  376.         return (-1);
  377.  
  378.     hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
  379.     hashp->HIGH_MASK = (nbuckets << 1) - 1;
  380.     hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
  381.         hashp->BSHIFT) + 1;
  382.  
  383.     nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
  384.     nsegs = 1 << __log2(nsegs);
  385.  
  386.     if (nsegs > hashp->DSIZE)
  387.         hashp->DSIZE = nsegs;
  388.     return (alloc_segs(hashp, nsegs));
  389. }
  390.  
  391. /********************** DESTROY/CLOSE ROUTINES ************************/
  392.  
  393. /*
  394.  * Flushes any changes to the file if necessary and destroys the hashp
  395.  * structure, freeing all allocated space.
  396.  */
  397. static int
  398. hdestroy(hashp)
  399.     HTAB *hashp;
  400. {
  401.     int i, save_errno;
  402.  
  403.     save_errno = 0;
  404.  
  405. #ifdef HASH_STATISTICS
  406.     (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
  407.         hash_accesses, hash_collisions);
  408.     (void)fprintf(stderr, "hdestroy: expansions %ld\n",
  409.         hash_expansions);
  410.     (void)fprintf(stderr, "hdestroy: overflows %ld\n",
  411.         hash_overflows);
  412.     (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
  413.         hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
  414.  
  415.     for (i = 0; i < NCACHED; i++)
  416.         (void)fprintf(stderr,
  417.             "spares[%d] = %d\n", i, hashp->SPARES[i]);
  418. #endif
  419.     /*
  420.      * Call on buffer manager to free buffers, and if required,
  421.      * write them to disk.
  422.      */
  423.     if (__buf_free(hashp, 1, hashp->save_file))
  424.         save_errno = errno;
  425.     if (hashp->dir) {
  426.         free(*hashp->dir);    /* Free initial segments */
  427.         /* Free extra segments */
  428.         while (hashp->exsegs--)
  429.             free(hashp->dir[--hashp->nsegs]);
  430.         free(hashp->dir);
  431.     }
  432.     if (flush_meta(hashp) && !save_errno)
  433.         save_errno = errno;
  434.     /* Free Bigmaps */
  435.     for (i = 0; i < hashp->nmaps; i++)
  436.         if (hashp->mapp[i])
  437.             free(hashp->mapp[i]);
  438.  
  439.     if (hashp->fp != -1)
  440.         (void)close(hashp->fp);
  441.  
  442.     free(hashp);
  443.  
  444.     if (save_errno) {
  445.         errno = save_errno;
  446.         return (ERROR);
  447.     }
  448.     return (SUCCESS);
  449. }
  450. /*
  451.  * Write modified pages to disk
  452.  *
  453.  * Returns:
  454.  *     0 == OK
  455.  *    -1 ERROR
  456.  */
  457. static int
  458. hash_sync(dbp, flags)
  459.     const DB *dbp;
  460.     u_int32_t flags;
  461. {
  462.     HTAB *hashp;
  463.  
  464.     if (flags != 0) {
  465.         errno = EINVAL;
  466.         return (ERROR);
  467.     }
  468.  
  469.     if (!dbp)
  470.         return (ERROR);
  471.  
  472.     hashp = (HTAB *)dbp->internal;
  473.     if (!hashp->save_file)
  474.         return (0);
  475.     if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
  476.         return (ERROR);
  477.     hashp->new_file = 0;
  478.     return (0);
  479. }
  480.  
  481. /*
  482.  * Returns:
  483.  *     0 == OK
  484.  *    -1 indicates that errno should be set
  485.  */
  486. static int
  487. flush_meta(hashp)
  488.     HTAB *hashp;
  489. {
  490.     HASHHDR *whdrp;
  491. #if BYTE_ORDER == LITTLE_ENDIAN
  492.     HASHHDR whdr;
  493. #endif
  494.     int fp, i, wsize;
  495.  
  496.     if (!hashp->save_file)
  497.         return (0);
  498.     hashp->MAGIC = HASHMAGIC;
  499.     hashp->VERSION = HASHVERSION;
  500.     hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
  501.  
  502.     fp = hashp->fp;
  503.     whdrp = &hashp->hdr;
  504. #if BYTE_ORDER == LITTLE_ENDIAN
  505.     whdrp = &whdr;
  506.     swap_header_copy(&hashp->hdr, whdrp);
  507. #endif
  508.     if ((lseek(fp, (off_t)0, SEEK_SET) == -1) ||
  509.         ((wsize = DB_write(fp, whdrp, sizeof(HASHHDR))) == -1))
  510.         return (-1);
  511.     else
  512.         if (wsize != sizeof(HASHHDR)) {
  513.             errno = EFTYPE;
  514.             hashp->errno = errno;
  515.             return (-1);
  516.         }
  517.     for (i = 0; i < NCACHED; i++)
  518.         if (hashp->mapp[i])
  519.             if (__put_page(hashp, (char *)hashp->mapp[i],
  520.                 hashp->BITMAPS[i], 0, 1))
  521.                 return (-1);
  522.     return (0);
  523. }
  524.  
  525. /*******************************SEARCH ROUTINES *****************************/
  526. /*
  527.  * All the access routines return
  528.  *
  529.  * Returns:
  530.  *     0 on SUCCESS
  531.  *     1 to indicate an external ERROR (i.e. key not found, etc)
  532.  *    -1 to indicate an internal ERROR (i.e. out of memory, etc)
  533.  */
  534. static int
  535. hash_get(dbp, key, data, flag)
  536.     const DB *dbp;
  537.     const DBT *key;
  538.     DBT *data;
  539.     u_int32_t flag;
  540. {
  541.     HTAB *hashp;
  542.  
  543.     hashp = (HTAB *)dbp->internal;
  544.     if (flag) {
  545.         hashp->errno = errno = EINVAL;
  546.         return (ERROR);
  547.     }
  548.     return (hash_access(hashp, HASH_GET, (DBT *)key, data));
  549. }
  550.  
  551. static int
  552. hash_put(dbp, key, data, flag)
  553.     const DB *dbp;
  554.     DBT *key;
  555.     const DBT *data;
  556.     u_int32_t flag;
  557. {
  558.     HTAB *hashp;
  559.  
  560.     hashp = (HTAB *)dbp->internal;
  561.     if (flag && flag != R_NOOVERWRITE) {
  562.         hashp->errno = errno = EINVAL;
  563.         return (ERROR);
  564.     }
  565.     if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
  566.         hashp->errno = errno = EPERM;
  567.         return (ERROR);
  568.     }
  569.     return (hash_access(hashp, flag == R_NOOVERWRITE ?
  570.         HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
  571. }
  572.  
  573. static int
  574. hash_delete(dbp, key, flag)
  575.     const DB *dbp;
  576.     const DBT *key;
  577.     u_int32_t flag;        /* Ignored */
  578. {
  579.     HTAB *hashp;
  580.  
  581.     hashp = (HTAB *)dbp->internal;
  582.     if (flag && flag != R_CURSOR) {
  583.         hashp->errno = errno = EINVAL;
  584.         return (ERROR);
  585.     }
  586.     if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
  587.         hashp->errno = errno = EPERM;
  588.         return (ERROR);
  589.     }
  590.     return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
  591. }
  592.  
  593. /*
  594.  * Assume that hashp has been set in wrapper routine.
  595.  */
  596. #ifdef macintosh
  597. static int
  598. hash_access(HTAB *hashp, ACTION action, DBT * key, DBT * val)
  599. #else
  600. static int
  601. hash_access(hashp, action, key, val)
  602.     HTAB *hashp;
  603.     ACTION action;
  604.     DBT *key, *val;
  605. #endif
  606. {
  607.     register BUFHEAD *rbufp;
  608.     BUFHEAD *bufp, *save_bufp;
  609.     register u_int16_t *bp;
  610.     register int n, ndx, off, size;
  611.     register char *kp;
  612.     u_int16_t pageno;
  613.  
  614. #ifdef HASH_STATISTICS
  615.     hash_accesses++;
  616. #endif
  617.  
  618.     off = hashp->BSIZE;
  619.     size = key->size;
  620.     kp = (char *)key->data;
  621.     rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
  622.     if (!rbufp)
  623.         return (ERROR);
  624.     save_bufp = rbufp;
  625.  
  626.     /* Pin the bucket chain */
  627.     rbufp->flags |= BUF_PIN;
  628.     for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
  629.         if (bp[1] >= REAL_KEY) {
  630.             /* Real key/data pair */
  631.             if (size == off - *bp &&
  632.                 memcmp(kp, rbufp->page + *bp, size) == 0)
  633.                 goto found;
  634.             off = bp[1];
  635. #ifdef HASH_STATISTICS
  636.             hash_collisions++;
  637. #endif
  638.             bp += 2;
  639.             ndx += 2;
  640.         } else if (bp[1] == OVFLPAGE) {
  641.             rbufp = __get_buf(hashp, *bp, rbufp, 0);
  642.             if (!rbufp) {
  643.                 save_bufp->flags &= ~BUF_PIN;
  644.                 return (ERROR);
  645.             }
  646.             /* FOR LOOP INIT */
  647.             bp = (u_int16_t *)rbufp->page;
  648.             n = *bp++;
  649.             ndx = 1;
  650.             off = hashp->BSIZE;
  651.         } else if (bp[1] < REAL_KEY) {
  652.             if ((ndx =
  653.                 __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
  654.                 goto found;
  655.             if (ndx == -2) {
  656.                 bufp = rbufp;
  657.                 if (!(pageno =
  658.                     __find_last_page(hashp, &bufp))) {
  659.                     ndx = 0;
  660.                     rbufp = bufp;
  661.                     break;    /* FOR */
  662.                 }
  663.                 rbufp = __get_buf(hashp, pageno, bufp, 0);
  664.                 if (!rbufp) {
  665.                     save_bufp->flags &= ~BUF_PIN;
  666.                     return (ERROR);
  667.                 }
  668.                 /* FOR LOOP INIT */
  669.                 bp = (u_int16_t *)rbufp->page;
  670.                 n = *bp++;
  671.                 ndx = 1;
  672.                 off = hashp->BSIZE;
  673.             } else {
  674.                 save_bufp->flags &= ~BUF_PIN;
  675.                 return (ERROR);
  676.             }
  677.         }
  678.  
  679.     /* Not found */
  680.     switch (action) {
  681.     case HASH_PUT:
  682.     case HASH_PUTNEW:
  683.         if (__addel(hashp, rbufp, key, val)) {
  684.             save_bufp->flags &= ~BUF_PIN;
  685.             return (ERROR);
  686.         } else {
  687.             save_bufp->flags &= ~BUF_PIN;
  688.             return (SUCCESS);
  689.         }
  690.     case HASH_GET:
  691.     case HASH_DELETE:
  692.     default:
  693.         save_bufp->flags &= ~BUF_PIN;
  694.         return (ABNORMAL);
  695.     }
  696.  
  697. found:
  698.     switch (action) {
  699.     case HASH_PUTNEW:
  700.         save_bufp->flags &= ~BUF_PIN;
  701.         return (ABNORMAL);
  702.     case HASH_GET:
  703.         bp = (u_int16_t *)rbufp->page;
  704.         if (bp[ndx + 1] < REAL_KEY) {
  705.             if (__big_return(hashp, rbufp, ndx, val, 0))
  706.                 return (ERROR);
  707.         } else {
  708.             val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
  709.             val->size = bp[ndx] - bp[ndx + 1];
  710.         }
  711.         break;
  712.     case HASH_PUT:
  713.         if ((__delpair(hashp, rbufp, ndx)) ||
  714.             (__addel(hashp, rbufp, key, val))) {
  715.             save_bufp->flags &= ~BUF_PIN;
  716.             return (ERROR);
  717.         }
  718.         break;
  719.     case HASH_DELETE:
  720.         if (__delpair(hashp, rbufp, ndx))
  721.             return (ERROR);
  722.         break;
  723.     default:
  724.         abort();
  725.     }
  726.     save_bufp->flags &= ~BUF_PIN;
  727.     return (SUCCESS);
  728. }
  729.  
  730. static int
  731. hash_seq(dbp, key, data, flag)
  732.     const DB *dbp;
  733.     DBT *key, *data;
  734.     u_int32_t flag;
  735. {
  736.     register u_int32_t bucket;
  737.     register BUFHEAD *bufp;
  738.     HTAB *hashp;
  739.     u_int16_t *bp, ndx;
  740.  
  741.     hashp = (HTAB *)dbp->internal;
  742.     if (flag && flag != R_FIRST && flag != R_NEXT) {
  743.         hashp->errno = errno = EINVAL;
  744.         return (ERROR);
  745.     }
  746. #ifdef HASH_STATISTICS
  747.     hash_accesses++;
  748. #endif
  749.     if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
  750.         hashp->cbucket = 0;
  751.         hashp->cndx = 1;
  752.         hashp->cpage = NULL;
  753.     }
  754.  
  755.     for (bp = NULL; !bp || !bp[0]; ) {
  756.         if (!(bufp = hashp->cpage)) {
  757.             for (bucket = hashp->cbucket;
  758.                 bucket <= hashp->MAX_BUCKET;
  759.                 bucket++, hashp->cndx = 1) {
  760.                 bufp = __get_buf(hashp, bucket, NULL, 0);
  761.                 if (!bufp)
  762.                     return (ERROR);
  763.                 hashp->cpage = bufp;
  764.                 bp = (u_int16_t *)bufp->page;
  765.                 if (bp[0])
  766.                     break;
  767.             }
  768.             hashp->cbucket = bucket;
  769.             if (hashp->cbucket > hashp->MAX_BUCKET) {
  770.                 hashp->cbucket = -1;
  771.                 return (ABNORMAL);
  772.             }
  773.         } else
  774.             bp = (u_int16_t *)hashp->cpage->page;
  775.  
  776. #ifdef DEBUG
  777.         assert(bp);
  778.         assert(bufp);
  779. #endif
  780.         while (bp[hashp->cndx + 1] == OVFLPAGE) {
  781.             bufp = hashp->cpage =
  782.                 __get_buf(hashp, bp[hashp->cndx], bufp, 0);
  783.             if (!bufp)
  784.                 return (ERROR);
  785.             bp = (u_int16_t *)(bufp->page);
  786.             hashp->cndx = 1;
  787.         }
  788.         if (!bp[0]) {
  789.             hashp->cpage = NULL;
  790.             ++hashp->cbucket;
  791.         }
  792.     }
  793.     ndx = hashp->cndx;
  794.     if (bp[ndx + 1] < REAL_KEY) {
  795.         if (__big_keydata(hashp, bufp, key, data, 1))
  796.             return (ERROR);
  797.     } else {
  798.         key->data = (u_char *)hashp->cpage->page + bp[ndx];
  799.         key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
  800.         data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
  801.         data->size = bp[ndx] - bp[ndx + 1];
  802.         ndx += 2;
  803.         if (ndx > bp[0]) {
  804.             hashp->cpage = NULL;
  805.             hashp->cbucket++;
  806.             hashp->cndx = 1;
  807.         } else
  808.             hashp->cndx = ndx;
  809.     }
  810.     return (SUCCESS);
  811. }
  812.  
  813. /********************************* UTILITIES ************************/
  814.  
  815. /*
  816.  * Returns:
  817.  *     0 ==> OK
  818.  *    -1 ==> Error
  819.  */
  820. extern int
  821. __expand_table(hashp)
  822.     HTAB *hashp;
  823. {
  824.     u_int32_t old_bucket, new_bucket;
  825.     int dirsize, new_segnum, spare_ndx;
  826.  
  827. #ifdef HASH_STATISTICS
  828.     hash_expansions++;
  829. #endif
  830.     new_bucket = ++hashp->MAX_BUCKET;
  831.     old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
  832.  
  833.     new_segnum = new_bucket >> hashp->SSHIFT;
  834.  
  835.     /* Check if we need a new segment */
  836.     if (new_segnum >= hashp->nsegs) {
  837.         /* Check if we need to expand directory */
  838.         if (new_segnum >= hashp->DSIZE) {
  839.             /* Reallocate directory */
  840.             dirsize = hashp->DSIZE * sizeof(SEGMENT *);
  841.             if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
  842.                 return (-1);
  843.             hashp->DSIZE = dirsize << 1;
  844.         }
  845.         if ((hashp->dir[new_segnum] =
  846.             (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
  847.             return (-1);
  848.         hashp->exsegs++;
  849.         hashp->nsegs++;
  850.     }
  851.     /*
  852.      * If the split point is increasing (MAX_BUCKET's log base 2
  853.      * * increases), we need to copy the current contents of the spare
  854.      * split bucket to the next bucket.
  855.      */
  856.     spare_ndx = __log2(hashp->MAX_BUCKET + 1);
  857.     if (spare_ndx > hashp->OVFL_POINT) {
  858.         hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
  859.         hashp->OVFL_POINT = spare_ndx;
  860.     }
  861.  
  862.     if (new_bucket > hashp->HIGH_MASK) {
  863.         /* Starting a new doubling */
  864.         hashp->LOW_MASK = hashp->HIGH_MASK;
  865.         hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
  866.     }
  867.     /* Relocate records to the new bucket */
  868.     return (__split_page(hashp, old_bucket, new_bucket));
  869. }
  870.  
  871. /*
  872.  * If realloc guarantees that the pointer is not destroyed if the realloc
  873.  * fails, then this routine can go away.
  874.  */
  875. static void *
  876. hash_realloc(p_ptr, oldsize, newsize)
  877.     SEGMENT **p_ptr;
  878.     int oldsize, newsize;
  879. {
  880.     register void *p;
  881.  
  882.     if (p = malloc(newsize)) {
  883.         memmove(p, *p_ptr, oldsize);
  884.         memset((char *)p + oldsize, 0, newsize - oldsize);
  885.         free(*p_ptr);
  886.         *p_ptr = p;
  887.     }
  888.     return (p);
  889. }
  890.  
  891. extern u_int32_t
  892. __call_hash(hashp, k, len)
  893.     HTAB *hashp;
  894.     char *k;
  895.     int len;
  896. {
  897.     int n, bucket;
  898.  
  899.     n = hashp->hash(k, len);
  900.     bucket = n & hashp->HIGH_MASK;
  901.     if (bucket > hashp->MAX_BUCKET)
  902.         bucket = bucket & hashp->LOW_MASK;
  903.     return (bucket);
  904. }
  905.  
  906. /*
  907.  * Allocate segment table.  On error, destroy the table and set errno.
  908.  *
  909.  * Returns 0 on success
  910.  */
  911. static int
  912. alloc_segs(hashp, nsegs)
  913.     HTAB *hashp;
  914.     int nsegs;
  915. {
  916.     register int i;
  917.     register SEGMENT store;
  918.  
  919.     int save_errno;
  920.  
  921.     if ((hashp->dir =
  922.         (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
  923.         save_errno = errno;
  924.         (void)hdestroy(hashp);
  925.         errno = save_errno;
  926.         return (-1);
  927.     }
  928.     /* Allocate segments */
  929.     if ((store =
  930.         (SEGMENT)calloc(nsegs << hashp->SSHIFT, sizeof(SEGMENT))) == NULL) {
  931.         save_errno = errno;
  932.         (void)hdestroy(hashp);
  933.         errno = save_errno;
  934.         return (-1);
  935.     }
  936.     for (i = 0; i < nsegs; i++, hashp->nsegs++)
  937.         hashp->dir[i] = &store[i << hashp->SSHIFT];
  938.     return (0);
  939. }
  940.  
  941. #if BYTE_ORDER == LITTLE_ENDIAN
  942. /*
  943.  * Hashp->hdr needs to be byteswapped.
  944.  */
  945. static void
  946. swap_header_copy(srcp, destp)
  947.     HASHHDR *srcp, *destp;
  948. {
  949.     int i;
  950.  
  951.     P_32_COPY(srcp->magic, destp->magic);
  952.     P_32_COPY(srcp->version, destp->version);
  953.     P_32_COPY(srcp->lorder, destp->lorder);
  954.     P_32_COPY(srcp->bsize, destp->bsize);
  955.     P_32_COPY(srcp->bshift, destp->bshift);
  956.     P_32_COPY(srcp->dsize, destp->dsize);
  957.     P_32_COPY(srcp->ssize, destp->ssize);
  958.     P_32_COPY(srcp->sshift, destp->sshift);
  959.     P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
  960.     P_32_COPY(srcp->last_freed, destp->last_freed);
  961.     P_32_COPY(srcp->max_bucket, destp->max_bucket);
  962.     P_32_COPY(srcp->high_mask, destp->high_mask);
  963.     P_32_COPY(srcp->low_mask, destp->low_mask);
  964.     P_32_COPY(srcp->ffactor, destp->ffactor);
  965.     P_32_COPY(srcp->nkeys, destp->nkeys);
  966.     P_32_COPY(srcp->hdrpages, destp->hdrpages);
  967.     P_32_COPY(srcp->h_charkey, destp->h_charkey);
  968.     for (i = 0; i < NCACHED; i++) {
  969.         P_32_COPY(srcp->spares[i], destp->spares[i]);
  970.         P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
  971.     }
  972. }
  973.  
  974. static void
  975. swap_header(hashp)
  976.     HTAB *hashp;
  977. {
  978.     HASHHDR *hdrp;
  979.     int i;
  980.  
  981.     hdrp = &hashp->hdr;
  982.  
  983.     M_32_SWAP(hdrp->magic);
  984.     M_32_SWAP(hdrp->version);
  985.     M_32_SWAP(hdrp->lorder);
  986.     M_32_SWAP(hdrp->bsize);
  987.     M_32_SWAP(hdrp->bshift);
  988.     M_32_SWAP(hdrp->dsize);
  989.     M_32_SWAP(hdrp->ssize);
  990.     M_32_SWAP(hdrp->sshift);
  991.     M_32_SWAP(hdrp->ovfl_point);
  992.     M_32_SWAP(hdrp->last_freed);
  993.     M_32_SWAP(hdrp->max_bucket);
  994.     M_32_SWAP(hdrp->high_mask);
  995.     M_32_SWAP(hdrp->low_mask);
  996.     M_32_SWAP(hdrp->ffactor);
  997.     M_32_SWAP(hdrp->nkeys);
  998.     M_32_SWAP(hdrp->hdrpages);
  999.     M_32_SWAP(hdrp->h_charkey);
  1000.     for (i = 0; i < NCACHED; i++) {
  1001.         M_32_SWAP(hdrp->spares[i]);
  1002.         M_16_SWAP(hdrp->bitmaps[i]);
  1003.     }
  1004. }
  1005. #endif
  1006.